// Api地址
const baseUrl = "https://api.zetikeji.com/api/";
 
function getHeader() {
	if (wx.getStorageSync("token")) {
    	return {
    		"content-type":"application/json",
    		"token":wx.getStorageSync("token")
    	};
	}
	// 没有设置token时,不需要带token参数
	return { "content-type":"application/json" };
}

// 显示没有icon的纯文字提示框
function showErrToast(e) {
	if (e) {
    	wx.showToast({ title:e, icon:"none", duration:1500 });
	}
}

// Api查询器
function getPromise(url, data, method) {
	return new Promise((resolve, reject) => {
    	wx.request({
    		url:`${baseUrl}${url}`,
    		header:getHeader(),
    		method:method,
    		data:data,
    		success:(res)=>{
    			if (res.data.code != undefined) {
            		resolve(res.data);
        		} else {
        			reject("糟糕, 出状况了!")
    			}
    		},
    		fail:(err)=>{
        		reject(err);
    		}
    	});
	}).catch(function (e) {
		showErrToast(e);
	});
}

// 标准组合
const http = {
	get:function (url, data) {
    // uploadFormIds()
    	return getPromise(url, data, "GET");
	},
	post:function (url, data) {
    	return getPromise(url, data, "POST");
	},
	put:function (url, data) {
    	return getPromise(url, data, "PUT");
	},
	delete:function (url, data) {
    	return getPromise(url, data, "DELETE");
	}
}
 
export default http;